package edu.gatech.wms.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import edu.gatech.wms.R;
import edu.gatech.wms.controllers.Login;
import edu.gatech.wms.controllers.UserManager;

public class UserDetailsActivity extends Activity
{
	private AlertDialog.Builder	builder;
	private AlertDialog			dialog;
	private TextView			name, contact;	
	private CheckBox			isAdmin, isLocked;
	private int					_id, userId;
	private boolean				isLocked_Prior, isAdmin_Prior;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_details);
		_id = getIntent().getIntExtra(Login.USER_KEY, 0);

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		userId = prefs.getInt(Login.ACTIVE_KEY, 0);

		name = (TextView) findViewById(R.id.user_details_name);
		contact = (TextView) findViewById(R.id.user_details_contact);
		isAdmin = (CheckBox) findViewById(R.id.isAdmincheckBox);
		isLocked = (CheckBox) findViewById(R.id.isLockedcheckBox);
		
		name.setText(UserManager.getUserFullName(_id));
		contact.setText(UserManager.getUserContact(_id));

		if (UserManager.getAdmin(_id)) {
			//admin.setText("Administrator: true");
			isAdmin.setChecked(true);
			isAdmin_Prior = true;
		} else {
			//admin.setText("Administrator: false");
			isAdmin.setChecked(false);
			isAdmin_Prior = false;
		}
		
		if (UserManager.getLockedStatus(_id)) {
			//locked.setText("Locked: true");
			isLocked.setChecked(true);
			isLocked_Prior = true;
		} else {
			//locked.setText("Locked: false");
			isLocked.setChecked(false);
			isLocked_Prior = false;
		}

		builder = new AlertDialog.Builder(this);

		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int id)
			{
				finish();
			}
		});

		findViewById(R.id.User_Detail_Submit).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				builder.setMessage("Are you sure you want to make Changes??")
						.setTitle("Edit User?");
	
				builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int id)
					{
						if (!isAdmin_Prior && isAdmin.isChecked()) {
							UserManager.setAdmin(_id, true);
						} else if (isAdmin_Prior && !isAdmin.isChecked()){
							UserManager.setAdmin(_id, false);
						}
				
						if (isLocked_Prior && !isLocked.isChecked()) {
							UserManager.resetLockout(_id);
						} else if (!isLocked_Prior && isLocked.isChecked()) {
							//Lock an Account? Do we want this ever?
						}
						finish();
					}
				});
	
				dialog = builder.create();
				dialog.show();
			}
		});
		
		findViewById(R.id.user_detail_delete).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				if (userId == _id)
					Toast.makeText(getBaseContext(), "You can't delete yourself, silly!",
							Toast.LENGTH_SHORT).show();
				else
				{
					builder.setMessage("Are you sure you want to delete this user's acount?")
							.setTitle("Delete User?");

					builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int id)
						{
							UserManager.deleteUser(_id);

							finish();
						}
					});

					dialog = builder.create();
					dialog.show();
				}

			}
		});

		findViewById(R.id.user_detail_cancel).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				finish();
			}
		});
	}
}
